<template>
    <div class="app-container1">
        <h1 style="text-align: center;">{{ titleHead }}</h1>
        <!-- 添加和修改表单 -->
        <el-form label-width="120px" style="width: 500px;" class="addForm">
            <el-form-item label="医院名称">
                <el-input v-model="hospitalSet.hosname"/>
            </el-form-item>
            
            <el-form-item label="医院编号">
                <el-input v-model="hospitalSet.hoscode"/>
            </el-form-item>
            
            <el-form-item label="api基础路径">
                <el-input v-model="hospitalSet.apiUrl"/>
            </el-form-item>
            
            <el-form-item label="联系人姓名">
                <el-input v-model="hospitalSet.contactsName"/>
            </el-form-item>
            
            <el-form-item label="联系人手机">
                <el-input v-model="hospitalSet.contactsPhone"/>
            </el-form-item>
            
            <el-form-item>
                <el-button type="primary" @click="saveOrUpdate()" icon="el-icon-check">保存</el-button>
                <el-button type="primary" @click="goBack()" icon="el-icon-back" class="backButton">返回</el-button>
            </el-form-item>
	    </el-form>
    </div>
  </template>
  
  <script>
  //引入js文件
  import hospSet_api from '@/api/hosp/hospSet'
  export default {
     data(){
        return{
            hospitalSet:{},
            titleHead:''
        }
     },

     created(){
        this.addOrEdit()
     },

     methods:{
        saveOrUpdate(){
         if(this.hospitalSet.id){
            this.update()
         }else{
            this.save()
         }
        },

        //保存方法
        save(){
            hospSet_api.addHospSet(this.hospitalSet).then(res => {
                //提示信息
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
                //返回主页面
                this.goBack()
            })
        },

        //修改方法
        update(){
            hospSet_api.update(this.hospitalSet).then(res => {
                //提示信息
                //提示信息
                this.$message({
                    message: '修改成功',
                    type: 'success'
                });
                //返回主页面
                this.goBack()
            })
        },

        //返回方法
        goBack(){
            this.$router.push("/hospSet/list")
        },

        //判断编辑or添加---初始化表单数据
        addOrEdit(){
            const id = this.$route.query.id
            if(id){
                hospSet_api.edit(id).then(res => {
                    this.hospitalSet = res.data
                    this.titleHead = "医院设置编辑"
                })
            }else{
                    this.hospitalSet = {}
                    this.titleHead = "医院设置添加"
            }
        }

     }
  }
  </script>
  
  <style>
  .app-container1{
    background-image:url(../../assets/hospSet/background-hospSetAdd.png);
    background-repeat:no-repeat;
    background-position:right bottom;
    height: 600px;
  }

  .addForm{
    margin-left: 240px;
  }

  .backButton{
    margin-left: 110px !important
}

  </style>